package com.ywgt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywgt.system.mapper.*;
import com.ywgt.system.pojo.*;
import com.ywgt.system.service.InStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.system.vo.InStockVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@Service
public class InStockServiceImpl extends ServiceImpl<InStockMapper, InStock> implements InStockService {

    @Autowired
    private InStockMapper inStockMapper;

    @Autowired
    private InStockInfoMapper inStockInfoMapper;

    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductStockMapper productStockMapper;
    @Override
    public Page<InStockVO> findInStockList(Integer pageNum, Integer pageSize, InStockVO inStockVO) {
        PageHelper.startPage(pageNum,pageSize);
        return inStockMapper.findInStockList(inStockVO);
    }

    @Override
    public InStock detail(Long id) {
        return inStockMapper.detail(id);
    }

    @Override
    public boolean delete(Long id) {
        LambdaQueryWrapper<InStock> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(InStock::getId,id);
        InStock inStock = inStockMapper.selectOne(queryWrapper);
        String inNum = inStock.getInNum();
        LambdaUpdateWrapper<InStockInfo> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(InStockInfo::getInNum,inNum);
        if (inStockMapper.deleteById(id)==1){
            if (inStockInfoMapper.delete(updateWrapper)==1) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean supplierInsert(InStockVO inStockVO, AuthUser authUser,Long supplierId) {
        InStock inStock=new InStock();
        String inNum= UUID.randomUUID().toString().substring(0,20);
        BeanUtils.copyProperties(inStockVO,inStock);
        inStock.setSupplierId(supplierId);
        inStock.setInNum(inNum);
        inStock.setOperator(authUser.getUsername());
        List<Integer> counts = inStockVO.getProducts().stream().map(Product::getCount).collect(Collectors.toList());
        int num = 0;
        for (Integer count : counts) {
            num+=count;
        }
        inStock.setProductNumber(num);
        inStockMapper.insert(inStock);
        List<Long> collect = inStockVO.getProducts().stream().map(Product::getId).collect(Collectors.toList());
        List<Product> products = productMapper.selectBatchIds(collect);
        for (Product product : products) {
            for (Product inStockVOProduct : inStockVO.getProducts()) {
                if (product.getId()==inStockVOProduct.getId()){
                    product.setCount(inStockVOProduct.getCount());
                }
            }
        }
        inStockInfoMapper.saveBatch(inNum,products);
        productStockMapper.increCount(products);
        return true;
    }

    public boolean newInsert(InStockVO inStockVO, AuthUser authUser){
        Supplier supplier=new Supplier();
        BeanUtils.copyProperties(inStockVO,supplier);
        supplierMapper.save(supplier);
        return supplierInsert(inStockVO,authUser,supplier.getId());
    }


}
